/**
 * 用户管理
 * @author wenchuan.li
 * @date 2018/12/08 17:01
 */
define(function () {

    var self,
        app = win.app,
        doc = document,
        layui = win.layui,
        table, userTable,
        vueData = {
            defaultProps: {
                children: 'children',
                label: 'name'
            },
            treeData: [],
            registerCount: 0,
            userCount: 0
        },
        table_where = {
            org_cascade_id: '0',
            org_id: '0',
            cascade: '1'
        },
        cols = [[
            // {field: 'ROW_ID', width: 60, title: '序号',unresize: true},
            {
                field: 'ACCOUNT',
                width: 100,
                title: '登录账号',
                fixed: 'left',
                search: 'default',
                searchOpts: {type: 'input', colX: 1}
            }
            , {field: 'NAME', width: 100, title: '姓名', sort: true, search: 'default'}
            , {field: 'ORG_NAME', width: 120, title: '所属组织'}
            , {
                field: 'sign',
                title: '所属角色',
                width: 150,
                filterDatas: [],
                search: 'default',
                searchOpts: {type: 'select', default: ''}
            }
            , {field: 'SEX', width: 60, title: '性别', filterDatas: app.datas_('sys_user_sex')}
            , {
                field: 'STATUS',
                width: 80,
                title: '用户状态',
                sort: true,
                filterDatas: app.datas_('sys_user_status'),
                search: true,
                searchOpts: {type: 'select', default: ''}
            }
            , {field: 'TYPE', width: 80, filterDatas: app.datas_('type'), title: '用户类型'}
            , {field: 'BIZ_CODE', width: 137, title: 'ERP人员', search: 'default'},
            {field: 'ADDRESS', width: 200, title: '联系地址'},
            {field: 'REMARK', width: 200, title: '备注'},
            {
                width: 50,
                title: '',
                toolbar: '#script-usersetting',
                fixed: 'right',
                align: 'center',
                unresize: true
            }
        ]],
        orgNodeData,
        selectNode,
        userSettingAction
    ;

    app.newModule({
        init: function () {
            self = this;
            self._super({
                _tag: 'user',
                _searchs: [{
                    id: 'user', tableId: 'user_table', datas: cols[0]
                }],
                _loadTplsFull: [{
                    dom: 'user_edit', tpl: 'system/user_edit'
                }, {
                    dom: 'user_roles', tpl: 'system/user_roles'
                }, {
                    dom: 'user_industry', tpl: 'system/user_industry'
                }]
            }, win);
        },
        initTable: function () {
            layui.use(['table'], function () {
                table = layui.table;
                userTable = table.render({
                    elem: app.getByid(doc, 'user_table'),
                    url: g_ctxPath + 'sys/user/getUserslist',
                    //exportsUrl: g_ctxPath + 'sys/user/exportsUser',
                    //exportsName: '用户数据',
                    importsUrl: g_ctxPath + 'sys/user/importsUser',
                    title: '用户信息表',
                    id: 'user_table',
                    //autoSort: false, //禁用前端自动排序 默认
                    toolbar: '#table-toolbar',
                    defaultToolbar: ['filter'],//, 'exports_import'
                    height: 'full',
                    cols: cols,
                    page: true,
                    where: table_where,
                    done: function (res, curr, count) {
                        self.getUserCount();
                    }
                });

                layui.laytpl(app.getByid(doc, 'script-usersetting-action').innerHTML).render({}, function (string) {
                    userSettingAction = string;
                });

                var tipsIndex = null;

                table.on('tool(user_table)', function (obj) {

                    // obj.updateOfEdit({STATUS:{
                    //     filterDatas:[{code:1, value:'1111'},{code:2, value:'2222'}],
                    //     code: 2
                    // }});
                    layer.tips(userSettingAction, obj.td, {
                        tips: [3, '']
                        , time: 5000
                        , anim: 500
                        , maxWidth: 120
                        , skin: 'layui-table-tips'
                        , success: function (layero, index) {
                            tipsIndex = index;
                            layero.find('li').on('click', function () {
                                switch (this.getAttribute('inx')) {
                                    case '0':
                                        self._openTplFull({
                                            tag: 'user_industry',
                                            tagData: obj,
                                            index: 2,
                                            openOpts: {
                                                title: '行业分类授权',
                                                area: ['900px', '560px']
                                            }
                                        });
                                        break;
                                    case '1':
                                        self._openTplFull({
                                            tag: 'user_roles',
                                            tagData: obj,
                                            index: 1,
                                            openOpts: {
                                                title: '分配角色',
                                                area: ['900px', '560px']
                                            }
                                        });
                                        break;
                                    case '2':
                                        self._openTplFull({
                                            tag: 'user_edit',
                                            tagData: {
                                                obj: obj,
                                                detail: false,
                                                orgNodeData: orgNodeData
                                            },
                                            index: 0,
                                            openOpts: {
                                                title: '用户信息编辑',
                                                area: ['720px', '550px']
                                            }
                                        });
                                        break;
                                    case '3':
                                        if (obj.data.ACCOUNT == 'admin') {
                                            app.showError('系统管理员，不允许删除！');
                                            return;
                                        }
                                        self.delUser(obj.data);
                                        break;
                                }
                                layer.close(index);
                                tipsIndex = null;
                            });
                        }
                    });
                    return false;
                });

                //监听行双击事件
                table.on('rowDouble(user_table)', function (obj) {
                    self._openTplFull({
                        tag: 'user_edit',
                        tagData: {
                            obj: obj,
                            detail: true
                        },
                        index: 0,
                        openOpts: {
                            title: '用户信息--编辑',
                            area: ['720px', '550px']
                        }
                    });

                });

                //监听事件
                table.on('toolbar(user_table)', function (obj) {
                    switch (obj.event) {
                        case 'add':
                            self._openTplFull({
                                tag: 'user_edit',
                                tagData: {
                                    is_add: true,
                                    org_id: selectNode.id,
                                    org_name: selectNode.name,
                                    orgNodeData: orgNodeData
                                },
                                openOpts: {
                                    title: '用户信息--新增',
                                    area: ['720px', '550px']
                                },
                                index: 0
                            });
                            break;
                        case 'delete':
                            layer.msg('删除');
                            break;
                    }
                });

                $(doc).on('click', function () {
                    tipsIndex && layer.close(tipsIndex);
                });

            });
        },
        reloadTableCurr: function () {
            self._reloadTable({id: 'user_table', where: table_where});
        },
        initTree: function () {
            // layui.use('tree', function () {
            //     app.ajax({
            //         xurl: 'sys/org/getOrgsTree',
            //         cb: function (result) {
            //             if (result.ok) {
            //                 orgNodeData = result.data.data;
            //                 selectNode = orgNodeData[0];
            //                 layui.tree({
            //                     elem: '#treeview',
            //                     nodes: orgNodeData.addMap({spread: true}),
            //                     click: function (node) {
            //                         selectNode = node;
            //                         table_where.org_cascade_id = node.code;
            //                         table_where.org_id = node.id;
            //                         self._reloadTable({id: 'user_table', curr: 1, where: table_where});
            //                     }
            //                 });
            //             }
            //         }
            //     });
            // });

            app.ajax({
                xurl: 'sys/org/getOrgsTree',
                async: false,
                cb: function (result) {
                    if (result.ok) {
                        orgNodeData = result.data.data;
                        vueData.treeData.pushArray(orgNodeData);
                        selectNode = orgNodeData[0];
                        table_where.org_cascade_id = selectNode.code;
                        table_where.org_id = selectNode.id;
                    }
                }
            });
        },
        getUserCount: function () {
            app.ajax({
                xurl: 'sys/user/getUserCount',
                xdata: {},
                cb: function (result) {
                    if (result.ok) {
                        vueData.registerCount = result.data.data.registerCount;
                        vueData.userCount = result.data.data.userCount;
                    }
                }
            });
        },
        delUser: function (data) {
            app.ajax({
                xurl: 'sys/user/delete',
                xdata: {
                    ID: data.ID, NAME: data.NAME
                },
                confirm: {
                    msg: '是否删除用户 [' + data.NAME + '] ？'
                },
                okmsg: '用户删除成功',
                loading: true,
                cb: function (result) {
                    if (result.ok) {
                        self.reloadTableCurr();
                    }
                }
            });
        },
        _ready: function () {
            vueData.serchData = table_where;
            this._super({
                vue: [{
                    el: '#page',
                    data: vueData,
                    searchId: 'user',
                    methods: {
                        handleNodeClick: function (node) {
                            selectNode = node;
                            table_where.org_cascade_id = node.code;
                            table_where.org_id = node.id;
                            self._reloadTable({id: 'user_table', curr: 1, where: table_where});
                        }, handleIconClick: function (field) {
                            app.log(field);
                        }, queryData: function () {
                            self._reloadTable({id: 'user_table', curr: 1, where: table_where});
                        }
                    }
                }]
            });
            // app.extend(table_where, searchObj.fields);
            self.initTree();
            self.initTable();
        },
        _lazy: function () {
            this._super();

            app.ajax({
                xurl: 'sys/role/getRoles',
                cb: function (result) {
                    if (result.ok) {
                        self._searchSetSelectDatas({searchId: 'user', field: 'sign', datas: result.data.data});
                    }
                }
            });

            var treeH = document.querySelector('.el-tree');
            var tableH = document.querySelector('.layui-form').clientHeight;
            treeH.style.height = (tableH + 2) + 'px';

            $(window).resize(function () {
                var tableH = document.querySelector('.layui-form').clientHeight;
                treeH.style.height = (tableH + 2) + 'px';
            })
        }
    });

});
